package com.blb.hello;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Hello服务的自动配置类
 */
//读取ConfigurationProperties的对象
@EnableConfigurationProperties(HelloProperties.class)
@Configuration
public class HelloAutoConfiguration {

    private HelloProperties helloProperties;

    //通过构造方法注入HelloProperties对象
    public HelloAutoConfiguration(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    //自动配置Hello服务
    @Bean
    //容器中不存在该对象，就创建对象，保证单例
    @ConditionalOnMissingBean
    public HelloService helloService(){
        HelloService helloService = new HelloService(helloProperties.getName(),
                helloProperties.getGender(),helloProperties.getAge());
        return helloService;
    }
}
